package net.melon.plugin.core.interceptor;

import net.esj.basic.core.view.ResponseStatusCode;
import net.esj.basic.core.view.tools.ActionContext;
import net.melon.plugin.core.PluginAware;

import org.apache.commons.lang.BooleanUtils;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 插件包默认拦截，默认拒绝所有访问
 * 请通过继承{@link  net.esj.basic.plugins.struts2.interceptor.AbstractPluginVisibleInterceptor}拦截器方法改变访问权限
 * @author Administrator
 *
 */
public class PluginDefaultBannedInterceptor extends AbstractInterceptor implements ResponseStatusCode{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4112070589780910794L;
	
	public static final String BANNED_REQUEST_KEY = "net.melon.plugin.struts2.banned";
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Object o = invocation.getAction();
		if(o instanceof PluginAware){
			PluginAware aware = (PluginAware) o;
			Object key = ActionContext.getSession().getAttribute(BANNED_REQUEST_KEY);
			boolean ispass = BooleanUtils.toBoolean(key==null ?"":key.toString());
			if(!ispass){
				return null;
			}
		}
		ActionContext.getSession().setAttribute(BANNED_REQUEST_KEY, "false");
		String ret = invocation.invoke();
		return ret;
	}

}
